package com.webservice.dao.returnDeal.impl;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;


import com.webservice.dao.returnDeal.IreturnDealDao;
import com.webservice.vo.returnDeal.ReturnSingleDetailVo;
import com.webservice.vo.returnDeal.ReturnSingleVo;

@Repository("ReturnDealDao")
public class ReturnDealDaoImpl implements IreturnDealDao {
	
	private static Logger logger = Logger.getLogger(ReturnDealDaoImpl.class);
	
	@Autowired
	@Qualifier("datatemplate")
	private JdbcTemplate jdbcTemplate;
	


	//生成返还单的方法(向返还单主表及明细表插入数据)
	@Override
	public boolean insertReturnSingle(ReturnSingleVo singleVo) {

		String sqlRS = "insert into return_single(id,single_code,status,supply_id,supply_name, supply_addr,supply_tel, order_code,dr,add_user,add_date, update_user, update_date,type) values (?,?,?,?,?,?,?,?,?,?,to_date(?,'yyyy-mm-dd'),?,to_date(?,'yyyy-mm-dd'),?)";
		String sqlRSD = "insert into return_single_detail(id,single_code,supply_id,product_id,product_name,return_number,dr,add_user,add_date,update_user,update_date) values(?,?,?,?,?,?,?,?,to_date(?,'yyyy-mm-dd'),?,to_date(?,'yyyy-mm-dd'))";

		Object[] params1 = new Object[] { singleVo.getSingleId(),
				singleVo.getSingleCode(), singleVo.getStatus(),
				singleVo.getSupplyId(), singleVo.getSupplyName(),
				singleVo.getSupplyAddr(), singleVo.getSupplyTel(),
				singleVo.getOrderCode(),  singleVo.getDr(),
				singleVo.getAddUser(), singleVo.getAddDate(),
				singleVo.getUpdateUser(), singleVo.getUpdateDate(), singleVo.getType()};
		
		logger.debug("向【返还单主表】插入数据SQL1: " + logSQL(sqlRS, params1));
		int i = jdbcTemplate.update(sqlRS, params1);
		if (i > 0) {
			for(int j=0;j<singleVo.getList().size();j++){
				ReturnSingleDetailVo vo=new ReturnSingleDetailVo();
				vo=singleVo.getList().get(j);
				Object[] params2 = new Object[] { vo.getSingleDetailId(),
						singleVo.getSingleCode(), singleVo.getSupplyId(),
						vo.getProductId(), vo.getProductName(),
						vo.getReturnNumber(),
						singleVo.getDr(), singleVo.getAddUser(), singleVo.getAddDate(),
						singleVo.getUpdateUser(), singleVo.getUpdateDate() };
				logger.debug("向【返还单明细表】插入数据SQL2:" + logSQL(sqlRSD, params2));
				jdbcTemplate.update(sqlRSD, params2);
			}
			return true;
		}
		return false;
	}
	
	
	public static String logSQL(String sql, Object args[]) {
		if (args == null)
			return sql;
		for (int i = 0; i < args.length; i++) {
			if (args[i] != null) {
				String param = args[i].toString().replace("$", "\\$");
				sql = sql.replaceFirst("\\?",
						param.length() == 0 ? param
								: "\\'" + param + "'");
			} else {
				sql = sql.replaceFirst("\\?", "null");
			}
		}
		return sql;
	}
	
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

}
